/*
 * @Author: 许警 11378748333@
 * @Date: 2024-08-12 09:12:28
 * @LastEditors: 许警 11378748333@
 * @LastEditTime: 2024-09-03 16:32:22
 * @FilePath: /hsxlsystem/src/hooks/useModal.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createVNode, render } from 'vue'
import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
import ConfirmDialog from 'ant-design-vue/es/modal/ConfirmDialog'

const useModal = {
  confirm: (opt = {}) => {
    const options = {
      title: '',
      content: '',
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        console.log('??')
        closeModal()
      },
      onCancel: () => {
        closeModal()
      },
      ...opt
      // 其他可能的选项...
    }

    const element = document.createDocumentFragment()
    const dialog = createVNode(ConfirmDialog, {
      type: 'confirm',
      visible: true,
      prefixCls: 'ant-modal',
      rootPrefixCls: 'ant',
      contentPrefixCls: 'ant-modal-confirm',
      ...options,
      onOk: () => {
        console.log('??')
        return new Promise((resolve, reject) => {
          options.onOk()
          // resolve()
        }).catch(() => console.log('Oops errors!'))
      },
      onCancel: () => {
        return new Promise((resolve, reject) => {
          console.log('options', options)

          options.onCancel()
          resolve()
        }).catch(() => console.log('Oops errors!'))
      },
      close() {
        closeModal()
      }
    })
    // 暴露的closeModal方法
    const closeModal = () => {
      dialog.component.props.visible = false
      // 如果有需要，可以添加额外的关闭逻辑
    }

    render(dialog, element)
    return {
      closeModal
    }
  }
}

export default useModal
